﻿using System;
using System.Collections.Generic;
using System.Linq;
using Pap.Data.Abstraction.Utils;

namespace Pap.Data.LToS.Repositories
{
    /// <summary>
    /// 
    /// </summary>
    public class MembershipRepositoryImpl : IMembershipRepository
    {
        /// <summary>
        /// 
        /// </summary>
        private IDatabase _Database;

        /// <summary>
        /// Initializes a new instance of the <see cref="MembershipRepositoryImpl"/> class.
        /// </summary>
        /// <param name="database">The database.</param>
        public MembershipRepositoryImpl(IDatabase database)
        {
            this._Database = database;
        }

        #region [ Users ]

        /// <summary>
        /// Gets the user by ID.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public IUser GetUserByID(Guid userID)
        {
            return _Database.Users.SingleOrDefault(t => t.UserID == userID);
        }

        /// <summary>
        /// Finds the users.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IUser> FindUsers(System.Linq.Expressions.Expression<Func<IUser, bool>> where)
        {
            return _Database.Users.Where(where);
        }

        /// <summary>
        /// Creates the user.
        /// </summary>
        /// <param name="user">The user.</param>
        public void CreateUser(IUser user)
        {
            _Database.Insert<IUser>(user);
        }

        /// <summary>
        /// Deletes the users.
        /// </summary>
        /// <param name="where">The where.</param>
        public void DeleteUsers(System.Linq.Expressions.Expression<Func<IUser, bool>> where)
        {
            IEnumerable<IUser> users = _Database.Users.Where(where);
            _Database.DeleteAll<IUser>(_Database.Users.Where(where));
        }

        /// <summary>
        /// Deletes the user roles.
        /// </summary>
        /// <param name="user">The user.</param>
        public void DeleteUserRoles(IUser user)
        {
            if (user.UsersRoles.Count() > 0)
            {
                _Database.DeleteAll<IUsersRole>(user.UsersRoles);
            }
        }

        /// <summary>
        /// Deletes the user projects.
        /// </summary>
        /// <param name="where">The where.</param>
        public void DeleteUserProjects(System.Linq.Expressions.Expression<Func<IUsersProject, bool>> where)
        {
            IEnumerable<IUsersProject> usersProjects = _Database.UsersProjects.Where(where);
            _Database.DeleteAll<IUsersProject>(_Database.UsersProjects.Where(where));
        }

        public IEnumerable<IUser> FindUsersByPermission(Permissions permission)
        {
            string strPermission = permission.ToString().ToLower();
            return from user in _Database.Users
                   join ur in _Database.UsersRole on user.UserID equals ur.UserID
                   join r in _Database.Roles on ur.RoleID equals r.RoleID
                   join rp in _Database.RolesPermission on r.RoleID equals rp.RoleID
                   join p in _Database.Permissions on rp.PermissionID equals p.PermissionID
                   where p.PermissionName.ToLower() == strPermission
                   select user;
        }

        #endregion

        #region [ Roles ]

        /// <summary>
        /// Gets the role by ID.
        /// </summary>
        /// <param name="roleID">The role ID.</param>
        /// <returns></returns>
        public IRole GetRoleByID(Guid roleID)
        {
            return _Database.Roles.SingleOrDefault(t => t.RoleID == roleID);
        }

        /// <summary>
        /// Finds the roles.
        /// </summary>
        /// <param name="Where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IRole> FindRoles(System.Linq.Expressions.Expression<Func<IRole, bool>> Where)
        {
            return this._Database.Roles.Where(Where);
        }

        /// <summary>
        /// Creates the role.
        /// </summary>
        /// <param name="Role">The role.</param>
        public void CreateRole(IRole Role)
        {
            this._Database.Insert<IRole>(Role);
        }

        /// <summary>
        /// Deletes the roles.
        /// </summary>
        /// <param name="Where">The where.</param>
        public void DeleteRoles(System.Linq.Expressions.Expression<Func<IRole, bool>> Where)
        {
            this._Database.DeleteAll<IRole>(this._Database.Roles.Where(Where));
        }

        /// <summary>
        /// Deletes the role permissions.
        /// </summary>
        /// <param name="role">The role.</param>
        public void DeleteRolePermissions(IRole role)
        {
            if (role.RolePermissions.Count() > 0)
            {
                _Database.DeleteAll<IRolesPermission>(role.RolePermissions);
            }
        }

        #endregion

        #region [ Permissions ]
        /// <summary>
        /// Gets all permissions.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<IPermission> GetAllPermissions()
        {
            return this._Database.Permissions;
        }

        /// <summary>
        /// Deletes the user committees.
        /// </summary>
        /// <param name="user">The user.</param>
        public void DeleteUserCommittees(IUser user)
        {
            /*
            if (user._Users_Committees.Count() > 0)
            {
                _Database.DeleteAll<IUsers_Committee>(user._Users_Committees);
            }
            */
        }

        #endregion


        #region [ Board Members ]

        /// <summary>
        /// Gets the member by ID.
        /// </summary>
        /// <param name="memberID">The member ID.</param>
        /// <returns></returns>
        public IBoardMember GetMemberByID(Guid memberID)
        {
            return _Database.BoardMembers.SingleOrDefault(t => t.BoardMemberID == memberID);
        }

        /// <summary>
        /// Finds the members.
        /// </summary>
        /// <param name="where">The where.</param>
        /// <returns></returns>
        public IEnumerable<IBoardMember> FindMembers(System.Linq.Expressions.Expression<Func<IBoardMember, bool>> where)
        {
            return _Database.BoardMembers.Where(where);
        }
        #endregion




    }
}
